package com.neptune.imapp.ui.compose.screen.mainPage.page

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.neptune.imapp.repository.vo.Apply

@Composable
fun Apply(applies: List<Apply>) {
    val applyState = remember {
        mutableStateOf(0)
    }
    val shadowBox = remember {
        mutableStateOf(0)
    }
    Column(modifier = Modifier.padding(bottom = 80.dp)) {
        Surface(shadowElevation = 10.dp) {
            Row(
                modifier = Modifier
                    .height(50.dp)
                    .wrapContentSize(Alignment.Center),
            ) {
                Text(text = "全部", modifier = Modifier
                    .clickable(
                        interactionSource = MutableInteractionSource(),
                        indication = null
                    ) {
                        applyState.value = 0
                        shadowBox.value = 0
                    }
                    .weight(1.0f)
                    .wrapContentSize(Alignment.Center)
                    .scale(animateFloatAsState(targetValue = if (shadowBox.value == 0) 1.5f else 1.0f).value))
                Text(text = "待处理", modifier = Modifier
                    .clickable(
                        interactionSource = MutableInteractionSource(),
                        indication = null
                    ) {
                        applyState.value = 1
                        shadowBox.value = 1
                    }
                    .weight(1.0f)
                    .wrapContentSize(Alignment.Center)
                    .scale(animateFloatAsState(targetValue = if (shadowBox.value == 1) 1.5f else 1.0f).value))
                Text(text = "已被拒绝", modifier = Modifier
                    .clickable(
                        interactionSource = MutableInteractionSource(),
                        indication = null
                    ) {
                        applyState.value = 2
                        shadowBox.value = 2
                    }
                    .weight(1.0f)
                    .wrapContentSize(Alignment.Center)
                    .scale(animateFloatAsState(targetValue = if (shadowBox.value == 2) 1.5f else 1.0f).value))
                Text(text = "已通过", modifier = Modifier
                    .clickable(
                        interactionSource = MutableInteractionSource(),
                        indication = null
                    ) {
                        applyState.value = 3
                        shadowBox.value = 3
                    }
                    .weight(1.0f)
                    .wrapContentSize(Alignment.Center)
                    .scale(animateFloatAsState(targetValue = if (shadowBox.value == 3) 1.5f else 1.0f).value))
            }
        }

        val list = when (applyState.value) {
            0 -> {
                applies
            }
            1 -> {
                applies.filter { it.status == 1 }
            }
            2 -> {
                applies.filter { it.status == 2 }
            }
            else -> {
                applies.filter { it.status == 3 }
            }
        }
        LazyColumn(
            content = {
                items(list.size) {
                    ApplyItem(apply = list[it])
                }
            },
            verticalArrangement = Arrangement.spacedBy(10.dp),
            contentPadding = PaddingValues(horizontal = 5.dp, vertical = 10.dp)
        )
    }
}


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ApplyItem(apply: Apply) {
    ElevatedCard(modifier = Modifier.fillMaxWidth()) {
        Column(
            modifier = Modifier.padding(10.dp),
            horizontalAlignment = Alignment.Start,
            verticalArrangement = Arrangement.spacedBy(5.dp)
        ) {
            Row {
                Text(
                    text = apply.name,
                    fontSize = 18.sp,
                    color = MaterialTheme.colorScheme.primary,
                    modifier = Modifier.weight(1.0f)
                )
                Text(
                    text = "${apply.minSalary}K--${apply.maxSalary}K",
                    color = MaterialTheme.colorScheme.primary,
                    fontSize = 20.sp,
                )
            }
            Row(horizontalArrangement = Arrangement.spacedBy(15.dp)) {
                Text(
                    text = apply.vacation,
                    color = MaterialTheme.colorScheme.secondary,
                    fontSize = 16.sp
                )
                Text(
                    text = "${apply.experience}年以上",
                    color = MaterialTheme.colorScheme.primary,
                    fontSize = 16.sp
                )
                Text(
                    text = apply.education,
                    color = MaterialTheme.colorScheme.primary,
                    fontSize = 16.sp
                )
                Text(
                    text = apply.address,
                    color = MaterialTheme.colorScheme.primary,
                    fontSize = 16.sp
                )
            }
            val list = apply.tag.split("-")
            LazyRow(
                horizontalArrangement = Arrangement.spacedBy(3.dp),
                content = {
                    items(list.size) {
                        Surface(
                            shape = RoundedCornerShape(15.dp),
                            color = MaterialTheme.colorScheme.surfaceTint
                        ) {
                            Text(
                                text = list[it],
                                modifier = Modifier
                                    .widthIn(min = 30.dp)
                                    .wrapContentSize(
                                        Alignment.Center
                                    )
                                    .padding(5.dp)
                            )
                        }
                    }
                })
        }
    }
}